package com.ciban.smarthome.module.ui;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.ciban.smarthome.R;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.engine.MonitorDHEngine;
import com.ciban.smarthome.module.entity.Equip_Monitor_Dh;

public class E_DHMonitorModifyActivity extends BaseActivity implements OnClickListener {

	private EditText et_name, et_ip, et_port, et_user, et_pwd;
	private Button bt_cancle, bt_save;

	private MonitorDHEngine engine;

	private Equip_Monitor_Dh monitor;
	private int equip_type=-1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_equip_dhmonitor_modify);

		initData();

		initViews();
	}

	private void initData() {
		if(getIntent().hasExtra("type")){
			equip_type = getIntent().getIntExtra("type",-1);
		}

		engine = new MonitorDHEngine(this);
	}

	private void initViews() {
		et_name = (EditText) findViewById(R.id.monitor_add_name);
		et_ip = (EditText) findViewById(R.id.monitor_add_ip);
		et_port = (EditText) findViewById(R.id.monitor_add_prot);
		et_user = (EditText) findViewById(R.id.monitor_add_user);
		et_pwd = (EditText) findViewById(R.id.monitor_add_pwd);

		bt_cancle = (Button) findViewById(R.id.monitor_add_cancle);
		bt_save = (Button) findViewById(R.id.monitor_add_save);

		setListeners();
	}

	private void setListeners() {

		bt_cancle.setOnClickListener(this);
		bt_save.setOnClickListener(this);

	}

	private void addMonitor() {
		String ip = et_ip.getText().toString().trim();
		String port = et_port.getText().toString().trim();
		String user = et_user.getText().toString().trim();
		String pwd = et_pwd.getText().toString().trim();
		String name = et_name.getText().toString().trim();


		Equip_Monitor_Dh info = new Equip_Monitor_Dh( user, pwd, ip, port);
		info.setType(equip_type);
		info.setName(name);
		info.setArea_id(baseApp.getNowArea().getId());
		boolean addNewMonitor = engine.addNewMonitor(info);
		if (addNewMonitor) {
			setResult(RESULT_OK);
			finish();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.monitor_add_cancle:
			finish();
			break;
		case R.id.monitor_add_save:
			addMonitor();
			break;
		}
	}

}
